package com.bjut.vendormgrsys.service.impl;

import com.bjut.vendormgrsys.service.MailService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;

@Service
public class MailServiceImpl implements MailService {

    public static final String SUBJECT = "账号开通通知";

    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private JavaMailSender javaMailSender;

    @Override
    @SneakyThrows
    public void send(String receivers, String userName, String userAccount, String initPassword, String url, String accountType) {
        String content = "<style class=\"fox_global_style\">\n" +
                "    div.fox_html_content { line-height: 1.5;}\n" +
                "    blockquote { margin-Top: 0px; margin-Bottom: 0px; margin-Left: 0.5em }\n" +
                "    ol, ul { margin-Top: 0px; margin-Bottom: 0px; list-style-position: inside; }\n" +
                "    p { margin-Top: 0px; margin-Bottom: 0px }\n" +
                "</style>%s，您好！<br><br>管理员为您开通了计算机科普平台%s账号，账号为 %s 初始密码为 %s, 请妥善保存<br><a href=\"%s\">点此登录</a><br>本邮件包含重要信息，<span style=\"font-weight:bold;\">请勿转发他人</span><br>系统自动发出，<span style=\"font-weight:bold;\">请勿直接回复</span>";

        // 使用spring mail发送邮件
        MimeMessage mail = javaMailSender.createMimeMessage();
        mail.addRecipients(Message.RecipientType.TO, InternetAddress.parse(receivers));
        mail.setFrom(new InternetAddress(from));
        mail.setSentDate(new Date());
        mail.setSubject(SUBJECT);
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setContent(String.format(content, userName, accountType, userAccount, initPassword, url), "text/html;charset=utf-8");
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mimeBodyPart);
        mail.setContent(multipart);
        javaMailSender.send(mail);
    }

    @Override
    @SneakyThrows
    public void sendAuditFailMsg(String receivers,String userName,String reason){
        String content = "<style class=\"fox_global_style\">\n" +
                "    div.fox_html_content { line-height: 1.5;}\n" +
                "    blockquote { margin-Top: 0px; margin-Bottom: 0px; margin-Left: 0.5em }\n" +
                "    ol, ul { margin-Top: 0px; margin-Bottom: 0px; list-style-position: inside; }\n" +
                "    p { margin-Top: 0px; margin-Bottom: 0px }\n" +
                "</style>%s，您好！<br><br>您申请的计算机科普服务平台会员账号审核未通过，原因：%s<br>非常感谢您对平台的关注与支持<br>本邮件包含重要信息，<span style=\"font-weight:bold;\">请勿转发他人</span><br>系统自动发出，<span style=\"font-weight:bold;\">请勿直接回复</span>";

        // 使用spring mail发送邮件
        MimeMessage mail = javaMailSender.createMimeMessage();
        mail.addRecipients(Message.RecipientType.TO, InternetAddress.parse(receivers));
        mail.setFrom(new InternetAddress(from));
        mail.setSentDate(new Date());
        mail.setSubject(SUBJECT);
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setContent(String.format(content, userName, reason), "text/html;charset=utf-8");
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mimeBodyPart);
        mail.setContent(multipart);
        javaMailSender.send(mail);
    }
}